<?php

namespace App\Http\Controllers;

use App\Model\Book;
use App\Model\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AdminController extends Controller
{
    //首页渲染
    public function index(){
        return view('admin',['isQuery' => 0]);
    }

    //书籍检索，分为带学生和不带学生两种
    public function getBooks(Request $request){
        $select = $request->input('query');
        $data = trim($request->input('data'));
        switch ($select){
            case 'username':
                $books = Book::join('book_student','book_student.book_id','=','books.id')
                    ->join('students','book_student.student_id','=','students.id')
                    ->select('students.*','books.*','book_student.*')
                    ->where('students.username',$data)
                    ->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 0]);
                break;
            case 'userid':
                $books = Book::join('book_student','book_student.book_id','=','books.id')
                    ->join('students','book_student.student_id','=','students.id')
                    ->select('students.*','books.*','book_student.*')
                    ->where('students.userid',$data)
                    ->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 0]);
                break;
            case 'bookid':
                $books = Book::where('bookid',$data)->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
            case 'name':
                $books = Book::where('name',$data)->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
            case 'author':
                $books = Book::where('author',$data)->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
            case 'publisher':
                $books = Book::where('publisher',$data)->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
            case 'type':
                $books = Book::where('type',$data)->get();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
            case 'all':
                $books = Book::all();
                return view('admin',['books' => $books,'isQuery' => 1,'isBookQuery' => 1]);
                break;
        }
    }

    //创建书籍
    public function createBook(Request $request){
        $validator = Validator::make($request->all(),[
            'bookid' => 'required',
            'name' => 'required',
            'author' => 'required',
            'publisher' => 'required',
            'type' => 'required'
        ],[
            'required' => ':attribute为必填项'
        ],[
            'bookid' => '书号',
            'name' => '书名',
            'author' => '作者',
            'publisher' => '出版社',
            'type' => '图书类别'
        ]);
        if ($validator->fails()){
            return redirect()->back()->withErrors($validator)->withInput();
        }
        Book::create($request->all());
        die('图书信息录入成功！');
    }

    public function deleteBook($id){
        $book = Book::find($id);
        $book->delete();
        die('书籍删除成功！');
    }

    //修改书籍模板渲染
    public function updateBookIndex($id){
        $book = Book::find($id);
        return view('input',['book' => $book]);
    }

    //更新指定id的书籍
    public function updateBook(Request $request,$id){
        $validator = Validator::make($request->all(),[
            'bookid' => 'required',
            'name' => 'required',
            'author' => 'required',
            'publisher' => 'required',
            'type' => 'required'
        ],[
            'required' => ':attribute为必填项'
        ],[
            'bookid' => '书号',
            'name' => '书名',
            'author' => '作者',
            'publisher' => '出版社',
            'type' => '图书类别'
        ]);
        if ($validator->fails()){
            return redirect()->back()->withErrors($validator)->withInput();
        }
        $book = Book::find($id);
        $book->update($request->all());
        die('书籍信息更新成功！');
    }
}
